package org.jsola.hr.core;

import com.alibaba.excel.exception.ExcelAnalysisException;
import lombok.extern.slf4j.Slf4j;
import org.jsola.core.Result;
import org.jsola.core.ResultCode;
import org.jsola.hr.exception.HrException;
import org.jsola.permission.core.PermissionExceptionTranslator;
import org.jsola.permission.exception.PermissionException;
import org.jsola.tax.exception.TaxException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 异常拦截
 */
@Slf4j
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE + 100)
public class HrExceptionTranslator extends PermissionExceptionTranslator {

    @ExceptionHandler(HrException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<String> processHrException(HrException e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage(), ResultCode.PARAM_ERROR);
    }

    @ExceptionHandler(PermissionException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    @Override
    public Result<String> handleBusinessException(PermissionException e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage(), ResultCode.PERMISSION_ERROR);
    }

    @ExceptionHandler(ExcelAnalysisException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<String> handleExcelAnalysisException(ExcelAnalysisException e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage(), ResultCode.PARAM_ERROR);
    }

    @ExceptionHandler(TaxException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<String> handleTaxException(TaxException e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage(), ResultCode.PARAM_ERROR);
    }
}
